const router = require('express').Router();
const mongoose = require('mongoose');
const AdminModel = mongoose.models['Admin'];
router.get('/', async function (req, res, next) {
    res.json({});
});

router.post('/checkLogin', async function (req, res, next) {
    let admin = await AdminModel.checkLogin(req.body.loginId, req.body.password);
    if (!!admin) {
        return res.data(admin);
    } else {
        next('登录失败！密码错误');
    }
});

router.post('/changePwd', async function (req, res, next) {
    let newPwd = req.body.newPwd;
    let rePwd = req.body.rePwd;
    if(newPwd != rePwd){
        next('两次输入新密码不一致！请重新输入！');
    }
    let admin = await AdminModel.changePwd(req.body.loginId, req.body.oldPwd, req.body.newPwd);
    if (!!admin) {
        return res.data(admin);
    } else {
        next('原始密码输入错误！');
    }
});

module.exports = router;